/*在goLogger的基础上自己再封装一层，直接默认配好gologger的各项配置*/
package logging

import (
	"fmt"

	goLogger "github.com/phachon/go-logger"
)

type logger struct {
	Logger *goLogger.Logger
}

var DefaultLogger = NewLogger()

func NewLogger() *logger {
	//loggerInstance为本包中基于goLoggerInstance封装的实例
	var loggerInstance *logger = new(logger)
	goLoggerInstance := goLogger.NewLogger()
	loggerInstance.Logger = goLoggerInstance

	//以下代码仍使用go_logger包中所提供的方法配置日志输出

	//设置控制台输出格式
	goLoggerInstance.Detach("console")
	consoleConfig := &goLogger.ConsoleConfig{
		Color:      true,
		JsonFormat: false,
		Format:     "",
	}
	goLoggerInstance.Attach("console", goLogger.LOGGER_LEVEL_DEBUG, consoleConfig)

	//设置文件输出格式
	fileConfig := &goLogger.FileConfig{
		LevelFileName: map[int]string{
			goLoggerInstance.LoggerLevel("error"): "./log/error.log",
			goLoggerInstance.LoggerLevel("info"):  "./log/info.log",
		},
		MaxSize:    0,
		MaxLine:    1000000,
		DateSlice:  "d",
		JsonFormat: false,
		Format:     "",
	}
	goLoggerInstance.Attach("file", goLogger.LOGGER_LEVEL_DEBUG, fileConfig)

	return loggerInstance
}

func (l *logger) Infoln(a ...interface{}) {
	l.Logger.Infof("%s", fmt.Sprintln(a...))
}

func (l *logger) Error(err error, format string, a ...interface{}) {
	newFormat := fmt.Sprintf("[err_msg: %s]", err.Error()) + format

	l.Logger.Errorf(newFormat, a...)
}

func (l *logger) Errorf(format string, a ...interface{}) {
	l.Logger.Errorf(format, a...)
}

func (l *logger) Info(format string, a ...interface{}) {
	l.Logger.Infof(format, a...)
}

func (l *logger) Infof(format string, a ...interface{}) {
	l.Logger.Infof(format, a...)
}

func (l *logger) Warning(msg string) {
	l.Logger.Warning(msg)
}

func (l *logger) Warningf(format string, a ...interface{}) {
	l.Logger.Warningf(format, a...)
}

func (l *logger) Debug(msg string) {
	l.Logger.Debug(msg)
}

func (l *logger) Debugf(format string, a ...interface{}) {
	l.Logger.Debugf(format, a...)
}
